﻿package com.ostrich.manpower.service.impl;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.base.util.DateUtil;
import com.base.util.SessionUtils;
import com.base.util.StringUtil;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrBirgiftinfo;
import com.ostrich.manpower.bean.TostHrCheckinlog;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.common.StringUtils;
import com.ostrich.manpower.service.TostHrBirgiftinfoDao;
import com.ostrich.system.analysis.ParaminfoUtil;
import com.ostrich.system.bean.TostSysParaminfo;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostHrBirgiftinfoService")
public class TostHrBirgiftinfoService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostHrBirgiftinfoService.class);

	@Autowired
    private TostHrBirgiftinfoDao<T> dao;

	public TostHrBirgiftinfoDao<T> getDao() {
		return dao;
	}
	@Autowired
	TostHrBaseinfoService<TostHrBaseinfo> tosthrbaseinfoservice;
	
	public void setBirgiftByHrids(String hsbMonth,String hrids,Integer noformal,Integer formal) throws Exception{
		String[] hridsstr = hrids.split(",");
		for(int i=0;hridsstr!=null && i<hridsstr.length;i++){
			TostHrBirgiftinfo bf = new TostHrBirgiftinfo();
			String[] hridstr = hridsstr[i].split("#");//获取hrid对象，并且分割为转正或使用期，用#号分割，编辑在JS文件中
			//转化hrid为Integer的对象
			Integer hrid = (hridstr[0]!=null&&StringUtil.isNumeric(hridstr[0]))?Integer.parseInt(hridstr[0]):null;
			if(hrid==null)
				continue;
			//根据hrid查出对应的人员信息，并且对生日礼金表进行插入，add添加会进行事物的回滚操作。
			TostHrBaseinfo tb = (TostHrBaseinfo) tosthrbaseinfoservice.queryById(hrid);
			bf.setHrId(hrid);
			bf.setHbrCompany(tb.getHrCompany());
			bf.setHbrDepart(tb.getHrDepart());
			bf.setHbrDatebirth(tb.getHrIdcard().substring(6, 14));
			bf.setHbrName(tb.getHrName());
			bf.setHbrMonth(hsbMonth);
			bf.setHbrGrade(tb.getHrGrades());
			//判断为1是表示转正，0是表示试用期或者未转正
			if("1".equals(hridstr[1])){
				bf.setHbrSalary(new Float(formal));
			}else{
				bf.setHbrSalary(new Float(noformal));
			}
			//设置转正状态 0为未转正，1表示转正
			bf.setHbrPositive(hridstr[1]);
			getDao().add((T) bf);
		}
		
	}
	public Integer isHavalastmm(){
		Integer lenth=getDao().isHavalastmm();
		return lenth;
		
	}
	public void delLastBirgift(){
		getDao().delLastBirgift();
	}
	
	public String setBirgiftByFilepath(String filepath) throws Exception {
		InputStream fis = new FileInputStream(filepath);
		HSSFSheet sh = ImportUtil.getSheet(fis);
		String msg = setBirgiftBySheet(sh);// 写数据库的方法
		return msg;
	}
	
		//导入
		private String setBirgiftBySheet(HSSFSheet sheet) throws Exception{
			HSSFRow row=null;
			int colnum=6;
			String msg ="";
			for(int j=2;j<sheet.getPhysicalNumberOfRows();j++){
				row=sheet.getRow(j);
				//先匹配个人编号
				String iscontinut = (row!=null)?ImportUtil.getCellValue(row.getCell(1)):null;
	       	 	TostHrBaseinfo bsinfo= (TostHrBaseinfo) tosthrbaseinfoservice.getBaseinfoByHrCode(iscontinut);
	       	 	TostHrBirgiftinfo bir=new TostHrBirgiftinfo();
				if(iscontinut!=null){
					if(bsinfo==null||bsinfo.getId() == null){
						msg +="第"+(j+1)+"行记录异常：工号："+iscontinut+"，姓名为："+ImportUtil.getCellValue(row.getCell(2))+"的没有正常导入（工号不存在）。";
						continue;
					}
				}
				if(iscontinut!=null && iscontinut.length()>0 && bsinfo!=null && (ImportUtil.getCellValue(row.getCell(2))).equalsIgnoreCase(bsinfo.getHrName()) ){
					for(int i=0;i<colnum;i++){
						String str=ImportUtil.getCellValue(row.getCell(i));
						switch(i){
							//case 0:String hrname=str;tcl.setHrName(hrname);tcl.setHrId(bsinfo.getId());break;
							case 1:bir.setHrCode(bsinfo.getHrCode());bir.setHrId(bsinfo.getId());;break;
							case 2:bir.setHbrName(bsinfo.getHrName());break;
							case 3:bir.setHbrSalary(Float.parseFloat(str));break;
							case 5:str=(str!=null && str.lastIndexOf(".")>0)?str.substring(0,str.lastIndexOf(".")):str;
								 bir.setHbrMonth(str);break;
						}
					}
						//查询改用的该类型该月份 有数据
						TostHrBirgiftinfo birg= queryBirgiftByMonth(bir);
						if(birg!=null && birg.getId()!=null){
							bir.setId(birg.getId());
							update((T) bir);
						}else{
							add((T) bir);
						}
				 }else{
					msg +="第"+(j+1)+"行记录异常：工号："+iscontinut+"，姓名为："+ImportUtil.getCellValue(row.getCell(2))+"的没有正常导入（工号跟姓名不匹配）。";
		       	 	continue;
				 }
			}
			return msg;
		}
		
		public TostHrBirgiftinfo queryBirgiftByMonth(TostHrBirgiftinfo birgif){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("hrId", birgif.getHrId());
			map.put("hbrMonth", birgif.getHbrMonth());
			birgif =  (TostHrBirgiftinfo) getDao().queryBirgift(map);
			return birgif;
		}
		
		public TostHrBirgiftinfo queryBirgift(int hrId,String month){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("hrId", hrId);
			map.put("hbrMonth",month);
			return (TostHrBirgiftinfo) getDao().queryBirgift(map);
		}
}
